package dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import util.DataBaseConnection;

import bean.Provincia;
import dao.Entidad;

public class MySQLProvinciaDAO implements Entidad<Provincia> {

	
	@Override
	public Provincia insert(Provincia t) {
		/*Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="INSERT INTO Provincia(id_Prov,id_Depa,id_Pais,nomb_prov";
			sql = sql + ") VALUES(?,?,?,?)";
			PreparedStatement ps=con.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setInt(1, t.getId_prov());
			ps.setInt(2, t.getId_depa());
			ps.setInt(3, t.getId_pais());
			ps.setString(4, t.getNomb_prov());
			ps.executeUpdate();
			ResultSet rs=ps.getGeneratedKeys();
			if(rs.next()){
				int id=rs.getInt(1);
				t.setIdProvincia(id);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}*/
		return t;
	}

	@Override
	public int update(Provincia t) {
		/*Connection con=null;
		int rows = -1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="UPDATE Provincia SET nombre=?";
			sql = sql +	"WHERE id_Provincia=? and id_Depatamento=? and id_Pais=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setString(1, t.getNombreProvincia());
			ps.setInt(2, t.getIdProvincia());
			ps.setInt(3, t.getIdDepartamento());
			ps.setInt(4, t.getIdPais());
					
			rows =ps.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}*/
		return -1;
	}

	@Override
	public int delete(Provincia t) {
		/*
		Connection con=null;
		int rows=-1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="DELETE FROM Provincia WHERE id_Provincia=? and id_Departamento=? and id_Pais=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getIdProvincia());
			ps.setInt(2, t.getIdDepartamento());
			ps.setInt(3, t.getIdPais());
			rows=ps.executeUpdate();
			System.out.println(rows);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}*/
		return -1;
	}

	@Override
	public Provincia findById(Provincia t) {
		/*Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="SELECT * FROM Provincia pr, Departamento d, Pais pa" +
					"WHERE pr.id_ Provincia=? and pr.id_Departamento = d.id_Departamento " +
					"and pa.id_Pais = pr.id_Pais and d.id_Pais = pr.id_Pais";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getIdProvincia());
			ResultSet rs=ps.executeQuery();
			while(rs.next()){
				t.setIdProvincia(rs.getInt("id_Provincia"));
				t.setNombreProvincia(rs.getString("nombre"));
				t.setIdDepartamento(rs.getInt("id_Departamento"));
				t.setNombreDepartamento(rs.getString("nombre"));
				t.setIdPais(rs.getInt("id_Pais"));
				t.setNombrePais(rs.getString("nombre"));		
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}*/
		return t;
	}

	@Override
	public List<Provincia> listAll() {
		
		/* List<Provincia> provincia = new ArrayList<Provincia>();
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="SELECT * FROM Provincia where " +
					"pr.id_Departamento = d.id_Departamento " +
					"and pa.id_Pais = pr.id_Pais and d.id_Pais = pr.id_Pais";
			Statement st=con.createStatement();
			ResultSet rs=st.executeQuery(sql);
			while(rs.next()){
				Provincia t = new Provincia();
				t.setIdProvincia(rs.getInt("id_Provincia"));
				t.setNombreProvincia(rs.getString("nombre"));
				t.setIdDepartamento(rs.getInt("id_departamento"));
				t.setNombreDepartamento(rs.getString("nombre"));
				t.setIdPais(rs.getInt("id_Pais"));
				t.setNombrePais(rs.getString("nombre"));
				provincia.add(t);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}*/
		return null;
	}

	@Override
	public List<Provincia> listAllCustom(Object... objects) {
		String id = (String)objects[0];
		List<Provincia> provincia = new ArrayList<Provincia>();
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			System.out.println(""+id);
			String sql="SELECT * FROM provincia p WHERE p.id_depa = "+id+" order by p.nomb_prov asc";
			Statement st=con.createStatement();
			ResultSet rs=st.executeQuery(sql);
			while(rs.next()){
				Provincia t = new Provincia();
				t.setId_prov(rs.getInt("id_prov"));
				t.setNomb_prov(rs.getString("nomb_prov"));
				t.setId_depa(rs.getInt("id_depa"));
				t.setId_pais(rs.getInt("id_pais"));
				provincia.add(t);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return provincia;
	}

}
